package com.atnjupt.web;

import com.atnjupt.bean.Book;
import com.atnjupt.bean.Page;
import com.atnjupt.service.impl.BookServiceImpl;
import com.atnjupt.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Wizarder
 * @create 2021-06-08 11:32
 */
public class ClientBookServlet extends BaseServlet{
    private  BookServiceImpl bookService = new BookServiceImpl();
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取请求的参数pageNo，pageSize。首次访问pageNo=1,pageSize采用默认值
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIEZ);
        //2、获取page对象
        Page<Book> page = bookService.page(pageNo, pageSize);
        page.setUrl("client/clientBookServlet?action=page");
        //3、保存到域数据
        request.setAttribute("page",page);
        //4、请求转发到/pages/client/index.jsp
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request,response);

    }
    protected void pageByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取请求的参数pageNo，pageSize。首次访问pageNo=1,pageSize采用默认值
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIEZ);
        int min = WebUtils.parseInt(request.getParameter("min"), 0);
        int max = WebUtils.parseInt(request.getParameter("max"), Integer.MAX_VALUE);
        //2、获取page对象
        Page<Book> page = bookService.pageByPrice(pageNo,pageSize,min,max);
        StringBuilder sb = new StringBuilder("client/clientBookServlet?action=pageByPrice");
        //分页跳转的时候得把输入的最大值和最小值带过来,不带来点击第二页max，min为默认值，所以就显示全部内容了
        if(request.getParameter("min") != null){
            sb.append("&min=").append(request.getParameter("min"));
        }
        if(request.getParameter("max") != null){
            sb.append("&max=").append(request.getParameter("max"));
        }
        page.setUrl(sb.toString());
        //3、保存到域数据
        request.setAttribute("page",page);
        //4、请求转发到/pages/client/index.jsp
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request,response);

    }
}
